package org.mushroomdb.security.queries;

import org.mushroomdb.engine.EvaluationEngine;
import org.mushroomdb.exception.EvaluationEngineException;
import org.mushroomdb.exception.RDBMSRuntimeException;
import org.mushroomdb.query.Query;
import org.mushroomdb.security.SecurityService;
import org.mushroomdb.security.User;
import org.mushroomdb.service.ServiceManager;


/**
 * Representatiom of teh "Alter User" Query
 */
public class AlterUserQuery extends SecurityQuery {
    private String userName;
    private String newPassword;

    /**
     * @param queryString
     */
    public AlterUserQuery(String query) {
        
        query = query.substring(query.indexOf("USER") + 4).trim();
        this.userName = query.substring(0, query.indexOf(" ")).trim();

        query = query
                .substring(query.indexOf("PASSWORD") + "PASSWORD".length())
                .trim();
        this.newPassword = query.substring(0, query.indexOf(";")).trim();
    }

   
    public Object execute(EvaluationEngine evaluationEngine) {
        
        SecurityService service = (SecurityService) ServiceManager.getInstance().getService(SecurityService.class);
        User user=null;
        user = service.getUserByName(this.userName);
        user.setPassword(this.newPassword);
        service.updateUser(user);
        return user;
    }

}
